package com.common.es.service;

import com.alibaba.fastjson.JSONObject;
import com.common.es.instance.EsInstance;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.common.es.config.EsConstant.*;

/**
 * @author 牧鱼
 * @Classname EsIndexOperation
 * @Description TODO
 * @Date 2021/5/10
 *
 * 索引相关操作逻辑
 */
@Service
public class EsIndexOperation {


    private RestHighLevelClient restClient = null;

    /**
     * 在ES实例当中获取ES连接对象
     * @param esInstance
     */
    public EsIndexOperation(EsInstance esInstance) {
        restClient = esInstance.getRestClient();
    }

    /**
     * 判断索引是否存在
     * @param indexName 索引名称
     * @return
     */
    public boolean checkIndex(String indexName) {
        try {
            return restClient.indices().exists(new GetIndexRequest(indexName), DEFAULT_OPTIONS);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Boolean.FALSE;
    }

    /**
     * 创建索引
     */
    public boolean createIndex(String indexName, Map<String, ?> mapping) {
        return createIndex(indexName, null, null, mapping);
    }

    /**
     * 创建索引
     */
    public boolean createIndex(String indexName, Map<String, ?> aliases, Map<String, ?> settings, Map<String, ?> properties) {
        if (checkIndex(indexName)) {
            return Boolean.TRUE;
        }
        try {
            CreateIndexRequest request = new CreateIndexRequest(indexName);
            if (aliases != null && aliases.size() > 0) {
                request.aliases(aliases);
            }
            if (settings != null && settings.size() > 0) {
                request.settings(settings);
            }
            if (properties != null && properties.size() > 0) {
                Map<String, Object> mapping = new HashMap<>();
                mapping.put(INDEX_MAPPING_PROPERTIES, properties);
                // 禁止自动添加字段
                mapping.put(INDEX_MAPPING_DYNAMIC, INDEX_MAPPING_DYNAMIC_STRICT);
                request.mapping(mapping);
            }
            CreateIndexResponse createIndexResponse = restClient.indices().create(request, DEFAULT_OPTIONS);
            System.out.println(JSONObject.toJSONString(createIndexResponse));
            return Boolean.TRUE;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Boolean.FALSE;
    }

    /**
     * 删除索引
     */
    public boolean deleteIndex(String indexName) {
        try {
            if (checkIndex(indexName)) {
                DeleteIndexRequest request = new DeleteIndexRequest(indexName);
                AcknowledgedResponse response = restClient.indices().delete(request, DEFAULT_OPTIONS);
                return response.isAcknowledged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Boolean.FALSE;
    }
}
